package com.example.entity;

import java.io.Serializable;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;

public class Payload implements Serializable {
    private static final long serialVersionUID = 5148921248042549583L;
    /**
     * 错误（服务内部字段）
     */
    private transient Map<String, String> errors;
    /**
     * 命令
     */
    private Command command;

    public final Command getCommand() {
        return command;
    }

    public final void setCommand(Command command) {
        this.command = command;
    }

    protected final void addError(String key, String name) {
        if (errors == null) {
            errors = new HashMap<>();
        }

        errors.put(key, name);
    }

    public final boolean hasError() {
        String errors = getErrors();
        return (errors != null) && (errors.length() > 0);
    }

    public final String getErrors() {
        if (errors == null) return null;

        StringBuilder sb = new StringBuilder();
        for (AbstractMap.Entry<String, String> entry : errors.entrySet()) {
            sb.append(String.format("%s %s\r\n", entry.getKey(), entry.getValue()));
        }
        return sb.toString();
    }

    /**
     * todo for sub class
     */
    public void validate() {
    }


    public static class Command implements Serializable {
        private static final long serialVersionUID = -3525555600789394488L;
        /**
         * 组代码
         */
        private Long code;
        /**
         * 子代码
         */
        private Long subCode;
        /**
         * 请求标识
         */
        private String requestId;
        /**
         * tag
         */
        private String tag;

        public Long getCode() {
            return code;
        }

        public void setCode(Long code) {
            this.code = code;
        }

        public Long getSubCode() {
            return subCode;
        }

        public void setSubCode(Long subCode) {
            this.subCode = subCode;
        }

        public String getRequestId() {
            return requestId;
        }

        public void setRequestId(String requestId) {
            this.requestId = requestId;
        }

        public String getTag() {
            return tag;
        }

        public void setTag(String tag) {
            this.tag = tag;
        }
    }
}